package cn.tedu.imop.stock.dao.repository.impl;

import cn.tedu.imop.stock.dao.mapper.CouponStockMapper;
import cn.tedu.imop.stock.dao.repository.CouponStockRepository;
import cn.tedu.imop.stock.entity.CouponStock;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.stereotype.Repository;

import java.sql.Wrapper;

@Repository
public class CouponStockRepositoryImpl implements CouponStockRepository {

    private CouponStockMapper couponStockMapper;
    public CouponStockRepositoryImpl(CouponStockMapper couponStockMapper) {
        this.couponStockMapper = couponStockMapper;
    }
    @Override
    public int insertCouponStock(CouponStock stock) {
        return couponStockMapper.insert(stock);
    }

    @Override
    public CouponStock selectCouponStock(Integer couponId) {
        QueryWrapper<CouponStock> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("coupon_id", couponId);
        CouponStock couponStock = couponStockMapper.selectOne(queryWrapper);
        return couponStock;
    }

    @Override
    public int updateNumByCouponId(Integer couponId) {
        UpdateWrapper<CouponStock> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("coupon_id", couponId);
        updateWrapper.setSql("num = num - 1");
        return couponStockMapper.update(updateWrapper);
    }
}
